//
//  ReplyCell.m
//  XiaoHongShu
//
//  Created by Jason on 2022/7/29.
//

#import "ReplyCell.h"
#import "YYText/YYText.h"
#import <UIButton+LXMImagePosition.h>
#import "CommentCellFrame.h"
#import "CommentListModel.h"

@interface ReplyCell()
//用户头像
@property (nonatomic, weak) UIButton *userImageBtn;
//用户名
@property (nonatomic, weak) UIButton *userNameBtn;
//用户回复 text
@property (nonatomic, weak) YYLabel *replyTextLabel;
//@property (nonatomic, weak) UILabel *replyTextLabel;
//点赞按钮
@property (nonatomic, weak) UIButton *likeBtn;
//是否是作者
@property (nonatomic, weak) UILabel *isAuthorLabel;
@end

@implementation ReplyCell

-(void)setCommentCellFrame:(CommentCellFrame *)commentCellFrame {
    _commentCellFrame = commentCellFrame;
   
    //设置数据
    [self setupData];
    
    //设置frame
    [self setupFrame];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+(instancetype)replyCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"replyCell";
    ReplyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[ReplyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    //用户头像
    UIButton *userImageBtn = [[UIButton alloc]init];
//    userImageBtn.backgroundColor = kRedColor;
    userImageBtn.layer.cornerRadius = 10;
    userImageBtn.layer.masksToBounds = YES;
    [self.contentView addSubview:userImageBtn];
    [userImageBtn addTarget:self action:@selector(userImageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _userImageBtn = userImageBtn;
    
    
    
    //用户名
    UIButton *userNameBtn = [[UIButton alloc]init];
//    userNameBtn.backgroundColor = kYellowColor;
//    [userNameBtn setTitle:@"一站式 | 无人机表演 | 求婚" forState:UIControlStateNormal];
    [userNameBtn setTitleColor:kLightGrayColor forState:UIControlStateNormal];
    userNameBtn.titleLabel.font = kFont(12);
    userNameBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [userNameBtn.titleLabel sizeToFit];
    [self.contentView addSubview:userNameBtn];
    [userNameBtn addTarget:self action:@selector(userNameBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _userNameBtn = userNameBtn;
    
    
    //用户评论 text
    YYLabel *replyTextLabel = [YYLabel new];
    replyTextLabel.numberOfLines = 0;
    replyTextLabel.font = kFont(14);
    replyTextLabel.displaysAsynchronously = YES;
//    replyTextLabel.backgroundColor = kGreenColor;
    [self.contentView addSubview:replyTextLabel];
    _replyTextLabel = replyTextLabel;
    
    
//    UILabel *replyTextLabel = [UILabel new];
//    replyTextLabel.numberOfLines = 0;
//    replyTextLabel.font = kFont(14);
//  //  replyTextLabel.displaysAsynchronously = YES;
//    replyTextLabel.backgroundColor = kGreenColor;
//    [self addSubview:replyTextLabel];
//    _replyTextLabel = replyTextLabel;
//
    
    //用户点赞
    UIButton *likeBtn = [[UIButton alloc]init];
//    likeBtn.backgroundColor = kBlueColor;
    [likeBtn setImage:kImageName(@"kongxin") forState:UIControlStateNormal];
    [likeBtn setImagePosition:LXMImagePositionTop spacing:10];
    likeBtn.titleLabel.font = [UIFont systemFontOfSize:11.0];
//    likeBtn.imageView.backgroundColor = [UIColor redColor];
    [likeBtn setImageEdgeInsets:UIEdgeInsetsMake(-12, 5, 12, -5)];
   // likeBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//    [likeBtn setTitle:@"1001" forState:UIControlStateNormal];
    [likeBtn setTitleColor:kLightGrayColor forState:UIControlStateNormal];
    [self.contentView addSubview:likeBtn];
    _likeBtn = likeBtn;
    
    UILabel *isAuthorLabel = [[UILabel alloc]init];
    isAuthorLabel.text = @"作者";
    isAuthorLabel.font = kFont(10);
    isAuthorLabel.textAlignment = NSTextAlignmentCenter;
    isAuthorLabel.backgroundColor = kLightGrayColor;
//    isAuthorLabel.alpha = 0.3;
    isAuthorLabel.textColor = kDarkGrayColor;
    isAuthorLabel.layer.cornerRadius = 7;
    isAuthorLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:isAuthorLabel];
    _isAuthorLabel = isAuthorLabel;
}

- (void)userImageBtnClick {
    NSLog(@"userImageBtnClick");
}

- (void)userNameBtnClick {
    NSLog(@"userNameBtnClick");
}

//设置数据
- (void)setupData {
    //获取数据
    CommentListModel *commentListModel = _commentCellFrame.commentListModel;
    [_userImageBtn sd_setImageWithURL:[NSURL URLWithString:commentListModel.userImage] forState:UIControlStateNormal];
    
    [_userNameBtn setTitle:commentListModel.userName forState:UIControlStateNormal];
    
    _isAuthorLabel.hidden = commentListModel.isAuthor;
    
    CommentListModel *targetCommentModel = commentListModel.targetComment;
    if (targetCommentModel) {
        //获取回复的子评论
        _replyTextLabel.text = [NSString stringWithFormat:@"回复 %@: %@\n%@ %@",targetCommentModel.userName, commentListModel.content, commentListModel.lastModifiedAt, commentListModel.ipLocation];
        NSMutableAttributedString *contentAttr = [[NSMutableAttributedString alloc] initWithString:_replyTextLabel.text];
        //获取需要改变的文本区间
        NSRange userNameRange = NSMakeRange(3, targetCommentModel.userName.length);
        // 改变颜色
        [contentAttr addAttribute:NSForegroundColorAttributeName value:kLightGrayColor range:userNameRange];
        // 改变字体大小及类型
        [contentAttr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:15] range:userNameRange];
        NSUInteger firstLen = userNameRange.length + userNameRange.location + 2;
        NSUInteger commentLen = commentListModel.content.length;
        
        NSUInteger endLoc = _replyTextLabel.text.length;
        // 需要改变的区间
        NSRange range = NSMakeRange(firstLen + commentLen, endLoc - firstLen - commentLen);
        // 改变颜色
        [contentAttr addAttribute:NSForegroundColorAttributeName value:kLightGrayColor range:range];
        // 改变字体大小及类型
        [contentAttr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:10] range:range];
        [_replyTextLabel setAttributedText:contentAttr];
    } else {
        _replyTextLabel.text = [NSString stringWithFormat:@"%@\n%@ %@", commentListModel.content, commentListModel.lastModifiedAt, commentListModel.ipLocation];
        NSMutableAttributedString *contentAttr = [[NSMutableAttributedString alloc] initWithString:_replyTextLabel.text];
        NSUInteger commentLen = commentListModel.content.length;
        
        NSUInteger endLoc = _replyTextLabel.text.length;
        // 需要改变的区间
        NSRange range = NSMakeRange(commentLen, endLoc - commentLen);
        // 改变颜色
        [contentAttr addAttribute:NSForegroundColorAttributeName value:kLightGrayColor range:range];
        // 改变字体大小及类型
        [contentAttr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:10] range:range];
        
        [_replyTextLabel setAttributedText:contentAttr];
    }
    
    [_likeBtn setTitle:[NSString stringWithFormat:@"%ld", commentListModel.likeCount] forState:UIControlStateNormal];
}

#pragma mark - 设置frame
- (void)setupFrame {
    _userImageBtn.frame = _commentCellFrame.userImageBtnFrame;
    
    _userNameBtn.frame = _commentCellFrame.userNameBtnFrame;
    
    _isAuthorLabel.frame = _commentCellFrame.isAuthorLabelFrame;
    
    _replyTextLabel.frame = _commentCellFrame.commentTextLabelFrame;
    
    _likeBtn.frame = _commentCellFrame.likeBtnFrame;
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
